//index.js
//获取应用实例
const app = getApp()
var _$ = require('../../common/wxjs/wx_ajax.js');
const url = "yq_jbxx";
Page({
  data: {
    hasUserInfo: false,
    usertoken: null,
    formdata: {
      "m_name": '',
      "m_age": '',
      "m_account": ''
    }
  },
  fromdata: function (e) {
    let momname = e.detail.value.m_name;
    let momage = e.detail.value.m_age;
    let momaccount = e.detail.value.m_account;
    if (momname == '') {
      this.setData({ popErrorMsg: "请输入姓名" });
    } else if (momage == '') {
      this.setData({ popErrorMsg: "请输入年龄" });
    } else if (momaccount == '') {
      this.setData({ popErrorMsg: "请驶入户口所在地" });
    } else if (momname != '' && momage != '' && momaccount != '') {
      wx.navigateTo({
        url: '../jksc/jksc'
      })
    }
    this.ohShitfadeOut();
  },
  getdata:function(val){
    var that = this;
    var type = 0;
    var litToken = val;
    var dataobj = { type, litToken };
    var methods = "GET";
    var header = "application/json";
    wx.showLoading({
      title: '加载中',
    })
    _$.sendRrquest(url, methods, dataobj, header)
      .then(function (response) {
        setTimeout(function () {
          wx.hideLoading()
        }, 1000)
        console.log(response)
        that.setData({
          formdata: response.data.data.res
        });
      }, function (error) {
        setTimeout(function () {
          wx.hideLoading()
        }, 2000)
        wx.showToast({
          title: '网络错误，请稍后再试',
          duration: 2000
        })
        setTimeout(function () {
          wx.hideToast()
        }, 2000)
        console.log(error);
      });
  },
  ohShitfadeOut() {
    var fadeOutTimeout = setTimeout(() => {
      this.setData({ popErrorMsg: '' });
      clearTimeout(fadeOutTimeout);
    }, 3000);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    // 登录
    wx.login({
      success: res0 => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        // this.globalData.code = res0.code,
        wx.getUserInfo({
          success: res1 => {
            wx.request({
              url: 'https://wx.jinwenlei.cn/app/index.php?i=4&c=entry&m=baolan&do=lit_user&op=login&little=1',
              data: {
                'code': res0.code,
                'encryptedData': res1.encryptedData,
                'iv': res1.iv,
                'litName': 'shouce'
              },
              header: {
                'content-type': 'application/json' // 默认值
              },
              success: function (res) {
                console.log(res)
                wx.setStorage({
                  key: "token",
                  data: res.data.data.user.litToken,
                })
                that.setData({
                  hasUserInfo:true,
                  usertoken: res.data.data.user.litToken
                })
                that.getdata(res.data.data.user.litToken)
              }
            })
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

})